package cn.jinjimi.app.main.fragment;

import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.BaseController;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.MakeCardExitEvent;
import cn.jinjimi.app.controller.event.SendCardSucc;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.event.CustomChangeEvent;
import cn.jinjimi.app.custom.event.CustomInfoEvent;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.main.dialog.LoadingDialog;
import cn.jinjimi.app.main.extend.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;

/**
 * 发送生日卡成功后新增客户
 * Created by Jinsen on 2015/6/3.
 */
public class CardSendSuccFragment extends BaseFragment implements View.OnClickListener {
    String mBlessingsStr = null;
    String mName = null;
    String mId = null;
    String mCardId = null;
    String mIconUrl = null;

    ImageView mIconIv = null;

    TextView mNameTv = null;

    TextView mActionDescTv = null;

    Button mCancelBtn, mCommitBtn;

    boolean mHasUploadRecord = false;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_send_card_succ_layout;
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mName = getArguments().getString("name");
        mId = getArguments().getString("id");
        mCardId = getArguments().getString("cardId");
        mIconUrl = getArguments().getString("iconUrl");
        mBlessingsStr = getArguments().getString("blessings");

        mIconIv = (ImageView) findView().findViewById(R.id.send_card_succ_header_icon_imageView);

        mCancelBtn = (Button) findView().findViewById(R.id.send_card_succ_action_cancel_button);

        mCommitBtn = (Button) findView().findViewById(R.id.send_card_succ_action_commit_button);

        mActionDescTv = (TextView) findView().findViewById(R.id.send_card_succ_action_desc_textView);

        mNameTv = (TextView) findView().findViewById(R.id.send_card_succ_name_textView);

    }

    protected void initListener() {
        mCancelBtn.setOnClickListener(this);
        mCommitBtn.setOnClickListener(this);
    }

    protected void initData() {
        if (StringUtils.isNotEmpty(mIconUrl))
            mIconIv.setImageURI(Uri.parse(mIconUrl));

        mNameTv.setText(mName);
        mActionDescTv.setText("是否把" + mName + "新增到客户列表?");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_card_succ_action_cancel_button:
                uploadRecord();
                break;
            case R.id.send_card_succ_action_commit_button:
                addCustom();
                break;
        }
    }

    //提交发送卡片记录
    private void uploadRecord() {
        mHasUploadRecord = true;
        CustomerController.uploadSendCardRecord(mBlessingsStr, mId, mCardId);
        EventBus.getDefault().post(new SendCardSucc());
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
    }


    //新增客户
    private void addCustom() {
        LoadingDialog.show(getActivity(), "正在添加，请稍后");
        SimpleDateFormat yearformat = new SimpleDateFormat("yyyy-MM-dd");
        String birthday = yearformat.format(System.currentTimeMillis());

        Custom custom = new Custom(mName,
                birthday, 0, null,
                BaseController.SOURCE_APP,
                AccountManager.getInstance().getAccount().ssoid,
                Custom.TYPE_NORMAL_CUSTOMER,
                Custom.CUSTOM_GENDER_NOSET,
                Custom.CUSTOM_NOT_SET,
                null, 0, null, null, null, null, null);

        CustomsManager.getInstance().addCustom(custom, null, null);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomInfoEvent(CustomInfoEvent event) {
        LoadingDialog.dispose();
        switch (event.state) {
            case DataEvent.SUCC:
                Custom custom = event.data;

                ToastUtils.toastMessage(getActivity(), "添加成功");
                mId = String.valueOf(custom.customerId);
                uploadRecord();

                CustomChangeEvent addEvent = new CustomChangeEvent(CustomChangeEvent.EVENT_ADD);
                addEvent.data = custom;
                EventBus.getDefault().post(addEvent);
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(getActivity(), "添加失败，请稍后重试");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMakeCardExitEvent(MakeCardExitEvent event) {
        getActivity().finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mHasUploadRecord) {
            uploadRecord();
        }
    }
}
